package com.shenwei.yingdaoguanli.controller;

import com.shenwei.yingdaoguanli.entity.FileCleanLog;
import com.shenwei.yingdaoguanli.entity.FileStat;
import com.shenwei.yingdaoguanli.entity.dto.BackupLogDto;
import com.shenwei.yingdaoguanli.entity.dto.FileCleanLogDto;
import com.shenwei.yingdaoguanli.service.FileCleanLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author wang
 * @Date 2025/1/8 22:46
 * @PackageName:com.shenwei.yingdaoguanli.controller
 * @ClassName: FileStatController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/fileCleanLog")
public class FileCleanLogController {


    @Autowired
    private FileCleanLogService fileCleanLogService;

    @Value("${file.clearPath}")
    private String fileCleanLogPath;

    @Value("${file.clearType}")
    private String clearType;


    @GetMapping("/getFileStatList")
    public List<FileCleanLog> getFileStatList(FileCleanLogDto fileCleanLogDto) {
        if (fileCleanLogDto == null) {
            fileCleanLogDto.setCleanDate(LocalDate.now().toString());
        }
        return fileCleanLogService.selectFileCleanLog(fileCleanLogDto);
    }


    // 根据ID 查询详细信息
    @GetMapping("/getFileCleanLogById/{id}")
    public FileCleanLog getFileCleanLogById(@PathVariable("id") String id) {
        return fileCleanLogService.selectFileCleanLogById(id);
    }

    // 清理接口
    @PostMapping("/startClean")
    public String startClean(@RequestBody FileCleanLogDto fileCleanLogDto) {
        String directoryPath = fileCleanLogDto.getDirectoryPath();
        String fileType = fileCleanLogDto.getFileType();
        if (directoryPath == null || directoryPath.trim().length() == 0) {
            directoryPath = fileCleanLogPath;
        }
        if (fileType == null || fileType.trim().length() == 0) {
            fileType = clearType;

        }
        try {
            // 执行清理操作
            boolean result = fileCleanLogService.cleanFiles(directoryPath, fileType);

            if (result) {
                return "文件清理成功！";
            } else {
                return "没有找到符合条件的文件进行清理！";
            }
        } catch (Exception e) {
            return "清理过程中出现错误：" + e.getMessage();
        }
    }


}